﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FinalCRM.App_Code
{
    public class Documents
    {
        public string DocName {get; set;}
        public string FileName {get; set;}
        public string DocType {get; set;}
        public string CusName {get; set;}
        public string UserName {get; set;}
        public DateTime DateCreated {get; set;}
        public string Description {get; set;}

        public static void CreateDoc(Documents d)
        {
            SqlConnection con = null;
            try
            {
                con = new SqlConnection();
                con.ConnectionString = "Data Source=.;Initial Catalog=Final_CRM;Integrated Security=true";
                SqlCommand com = new SqlCommand();
                con.Open();
                com.Connection = con;
                com.CommandText = "INSERT INTO CusDocument(DocName,FileName,DocType,DateCreated,UserName,CustomerName,Description) VALUES (@DocName,@FileName,@DocType,@DateCreated,@UserName,@CustomerName,@Description)";
                com.Parameters.AddWithValue("@DocName", d.DocName);
                com.Parameters.AddWithValue("@FileName", d.FileName);
                com.Parameters.AddWithValue("@DocType", d.DocType);
                com.Parameters.AddWithValue("@DateCreated", d.DateCreated);
                com.Parameters.AddWithValue("@UserName", d.UserName);
                com.Parameters.AddWithValue("@CustomerName", d.CusName);
                com.Parameters.AddWithValue("@Description", d.Description);
                int rowsAffected = com.ExecuteNonQuery();
            }
            catch(SqlException e)
            {
                throw e;
            }
            finally
            {
                con.Close();
            }
        }
        public static DataTable ViewDoc()
        {
            SqlConnection conn = null;
            DataTable dt = null;
            try
            {
                conn = new SqlConnection();
                conn.ConnectionString = "Data Source=.;Initial Catalog=Final_CRM;Integrated Security=true";
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "Select * from CusDocument";
                SqlDataReader dr = comm.ExecuteReader();
                dt = new DataTable();
                dt.Load(dr);

                dr.Close();
            }
            catch (SqlException e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
            }
            return dt;
        }

        public static void UpdateDoc(Documents d)
        {
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection();
                con.ConnectionString = "Data Source=.;Initial Catalog=Final_CRM;Integrated Security=true;";
                SqlCommand comm = new SqlCommand();
                conn.Open();
                comm.Connection = conn;
                comm.CommandText = "Update CusDocument SET FileName=@FileName, DocType=@DocType, Description=@Description WHERE DocName=@DocName";
                comm.Parameters.AddWithValue("@FileName", d.FileName);
                comm.Parameters.AddWithValue("@DocType", d.DocType);
                comm.Parameters.AddWithValue("@Description", d.Description);

                int rows = comm.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
            }
        }

        public static void DeleteDoc(Documents d)
        {
            SqlConnection conn = null;

            try
            {
                conn = new SqlConnection();


            }
        }
    }
}